package pos.MessageBox;

import java.util.HashMap;
import pos.ControlCenter;
import pos.ICommand;
import pos.IModeController;
import pos.utils.IContainer;

/**
 *
 * @author plus
 */
public class MessageBoxController implements IModeController {

    private HashMap<String, ICommand> CommandsPool = new HashMap<String, ICommand>();
    private MessageBox box;
    private IModeController receiver;

    public MessageBoxController(IModeController receiver) {
        this.receiver = receiver;
    }


    public void setMode() {
    }

    public void showMessageBox(String title, String contect,
            String[] buttonsText, String[] eventNames) {
        box = new MessageBox(this, receiver,title,  contect,
            buttonsText, eventNames);
        BlendCommands();
        ControlCenter.preFrameInvoker.addSingleLisener(box);
        ControlCenter.eventBehavior.pushTarget(box, this);
        ControlCenter.dashBoardRoot.addChild(box);
    }

    public void showMessageBox(MessageBox box){
        this.box=box;
        this.box.setReceiver(this, receiver);
        BlendCommands();
        ControlCenter.preFrameInvoker.addSingleLisener(box);
        ControlCenter.eventBehavior.pushTarget(box, this);
        //ControlCenter.dashBoardRoot.detach();
        ControlCenter.dashBoardRoot.addChild(box);
        
    }
    public void disposeMode() {
        ControlCenter.eventBehavior.popTarget();
        ControlCenter.preFrameInvoker.removeSingleLisener(box);
        //ControlCenter.dashBoardTG.addChild(ControlCenter.dashBoardRoot);
        //box.detach();
        ControlCenter.dashBoardRoot.removeChild(box);
    }

    public Object doEvent(String EventName, Object Param) {
        if (EventName.equals("Cancel")){
            this.disposeMode();
            return null;
        }
        ICommand InvokedCommand=CommandsPool.get(EventName);
        if (InvokedCommand!=null)
            return InvokedCommand.execute(Param);
        else{
            this.disposeMode();
            receiver.doEvent(EventName, Param);
            return null;
        }
    }
    /**
     * 将事件名与Command对象绑定
     */
    private void BlendCommands() {
        CommandsPool.put("KeyEvent", new CommandKeyEvent((IContainer)box));
    }
}
